package com.cxm.qyyz.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ListDivideDecoration extends RecyclerView.ItemDecoration {

    private final Drawable mDivider;
    private final int mOrientation;
    private final Rect mBounds = new Rect();
    private boolean begin;
    private boolean end;

    public ListDivideDecoration(Context context,@DrawableRes int drawableResId) {
        mDivider = ContextCompat.getDrawable(context, drawableResId);
        mOrientation = RecyclerView.VERTICAL;
    }

    public ListDivideDecoration(Context context, int orientation,@DrawableRes int drawableResId) {
        mDivider = ContextCompat.getDrawable(context, drawableResId);
        mOrientation = orientation;
    }

    public ListDivideDecoration setShowBeginAndEnd(boolean begin,boolean end){
        this.begin = begin;
        this.end = end;
        return this;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() == null || mDivider == null) {
            return;
        }
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        canvas.save();
        final int left;
        final int right;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (begin && i == 0){
                final View child = parent.getChildAt(i);
                parent.getDecoratedBoundsWithMargins(child, mBounds);
                final int top = mBounds.top;
                final int bottom = mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (!end && i == childCount - 1){
                continue;
            }
            final View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, mBounds);
            final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
            final int top = bottom - mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        canvas.restore();
    }

    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        canvas.save();
        final int top;
        final int bottom;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
            canvas.clipRect(parent.getPaddingLeft(), top,
                    parent.getWidth() - parent.getPaddingRight(), bottom);
        } else {
            top = 0;
            bottom = parent.getHeight();
        }

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
            final int right = mBounds.right + Math.round(child.getTranslationX());
            final int left = right - mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        canvas.restore();
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,RecyclerView.State state) {
        if (mDivider == null) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            RecyclerView.Adapter<?> adapter = parent.getAdapter();
            if (layoutManager != null && adapter != null) {
                int childCount = adapter.getItemCount();
                if (mOrientation == LinearLayoutManager.VERTICAL){
                    if (layoutManager.findViewByPosition(0) == view){
                        if (begin){
                            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
                        }else {
                            outRect.set(0, 0, 0, 0);
                        }
                    }else if (layoutManager.findViewByPosition(childCount - 1) == view){
                        if (!end){
                            outRect.set(0, 0, 0, 0);
                        }else {
                            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
                        }
                    }else {
                        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
                    }
                }
            }
        }
    }
}